﻿using System;
using System.Collections.Generic;
using Syno.Core.Output.DataLoader;

namespace Syno.Core.Output
{
    public class OutputManager
    {
        private readonly List<IOutput> _output = new List<IOutput>();
        private readonly Dictionary<string, IDataHolder> _dataHolders;
        private readonly INVelocityTemplates _templater;
        public OutputManager()
        {
            _output = new List<IOutput>();
            _dataHolders = new Dictionary<string, IDataHolder>();
            _templater = new NVelocityTemplates();
        }

        public void AddOutput(IOutput output)
        {
            _output.Add(output);
        }

        public void AddDataHolder(IDataHolder dataHold)
        {
            _dataHolders.Add(dataHold.Id, dataHold);
        }

        public void ProcessAll()
        {
            
            foreach (var key in _dataHolders.Keys)
            {
                var dataHolder = _dataHolders[key];
                dataHolder.SetTemplater(_templater);
                _templater.AddContext(dataHolder.Id, dataHolder.Data);
            }
            foreach (var output in _output){
                
                output.SetTemplater(_templater);
                output.Process();
            }
        }

        public void AddParameters(IDictionary<string, string> parameters)
        {
            foreach (var parameter in parameters)
            {
                _templater.AddContext(parameter.Key, parameter.Value);
            }
        }
    }
}